package com.qianchang.microfinance.view.credit.data;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.api.BankCardDetailApi;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.bean.BindDataBean;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.HSBankCardBean;
import com.qianchang.microfinance.bean.UserLoginInfo;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.BeanUtils;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.home.HomeFragment;
import com.qianchang.microfinance.view.hsbindbank.HSBindBankCardActivity;
import com.qianchang.microfinance.view.hsbindbank.HSUnBindBankCardActivity;
import com.qianchang.microfinance.view.hsbindbank.HSupBankCardPhoneActivity;
import com.qianchang.microfinance.view.hsbindbank.HSupBankCardPswActivity;
import com.qianchang.microfinance.view.hsdialog.BankCardDetailDialog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.qianchang.microfinance.utils.BeanUtils.isAllFieldNull;

public class BankCardDetailActivity extends BaseActivity implements BankCardDetailDialog.OnDialogClickListener{

    @BindView(R.id.iv_bank_icon)
    ImageView ivBankIcon;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_card_type)
    TextView tvBankCardType;
    @BindView(R.id.tv_bank_card)
    TextView tvBankCard;
    @BindView(R.id.ll_bank_id)
    LinearLayout bankLinear;
    @BindView(R.id.tv_nobinding_id)
    TextView nobindText;

    private FindUserBasicInfoBean findUserBasicInfoBean;
    private HSBankCardBean bankCardBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card_detail);
        ButterKnife.bind(this);

        getInfo();   //2018.1.23注释掉迪蒙代码

    }

    @Override
    protected void onResume() {
        super.onResume();

        getBankList();

    }

    public void getInfo() {
        setTitleName(getString(R.string.title_my_bank_card));

        Bundle bundle = getBundle();
        if (bundle == null) return;
        findUserBasicInfoBean = (FindUserBasicInfoBean) bundle.getSerializable(ConstantAppInfo.BASIC_INFO_BEAN);

        //支付  获取银行卡信息 2018.1.23占时注释掉迪蒙代码
        /****
        BankCardDetailApi.getBankCardDetail(this, new BankCardDetailApi.BankCardDetailCallBack() {
            @Override
            public void refresh(BankCardBean bankCardBean) {
                BankCardDetailActivity.this.refresh(bankCardBean);
            }
        });
        **************/


    }

    private void refresh(BankCardBean bean) {
        if (bean == null) return;
        tvBankName.setText(bean.getBankName());
        tvBankCardType.setText("储蓄卡");
        String bankCardNumber = bean.getBankCardNumber();
        if (!TextUtils.isEmpty(bankCardNumber) && bankCardNumber.length() > 4) {
            tvBankCard.setText(bankCardNumber.substring(bankCardNumber.length() - 4, bankCardNumber.length()));
        }
        //this会造成: You cannot start a load for a destroyed activity;改成this.getApplicationContext()
        Glide.with(this.getApplicationContext()).load(DMConstant.Config.BASE_URL + bean.getBankImgUrl()).error(R.drawable.img_wdyhk).into(ivBankIcon);
    }




    //获取银行卡列表   2018.1.23鸿胜新增
    private void getBankList(){
        setTitleName(getString(R.string.title_my_bank_card));
        setRightContent("管理");

        Bundle bundle = getBundle();
        if (bundle == null) return;
        findUserBasicInfoBean = (FindUserBasicInfoBean) bundle.getSerializable(HomeFragment.ENTITY_KEY);

        UserLoginInfo userLoginInfo = DMApplication.getInstance().getmUserLoginInfo();
        String usid = userLoginInfo.getUserId();
        HttpParams params = new HttpParams();
        params.put("userId",usid);
        show();

        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_BANKCARD, params, new RequestCallBack<HSBankCardBean>() {

            @Override
            public void onSuccess(JsonResult<HSBankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    bankCardBean = response.getObject();
                    hsRefresh(bankCardBean);
                }else{
                    ToastUtil.getInstant().show(BankCardDetailActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                ToastUtil.getInstant().show(BankCardDetailActivity.this, msg);
                dismiss();
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    boolean isNull = false;
    private void hsRefresh(HSBankCardBean bean) {
        if (bean == null) return;
        String cardName = bean.getBankCardName();
        if(cardName != null){
            isNull = true;
            bankLinear.setVisibility(View.VISIBLE);
            nobindText.setVisibility(View.GONE);
            if(cardName.contains("·")){
                String name = cardName.substring(0,cardName.indexOf("·"));
                tvBankName.setText(name);
                String cardType = cardName.substring(cardName.indexOf("·") + 1,cardName.length());
                tvBankCardType.setText(cardType);
            }else {
                tvBankName.setText(bean.getBankCardName());
                tvBankCardType.setText("储蓄卡");
            }
            String bankCardNumber = bean.getBankCard();
            if (!TextUtils.isEmpty(bankCardNumber) && bankCardNumber.length() > 4) {
                tvBankCard.setText(bankCardNumber.substring(bankCardNumber.length() - 4, bankCardNumber.length()));
            }
        }else {
            isNull = false;
            bankLinear.setVisibility(View.GONE);
            nobindText.setVisibility(View.VISIBLE);
        }

        //this会造成: You cannot start a load for a destroyed activity;改成this.getApplicationContext()
//        Glide.with(this.getApplicationContext()).load(DMConstant.Config.BASE_URL + bean.getBankImgUrl()).error(R.drawable.img_wdyhk).into(ivBankIcon);
    }


    @OnClick({R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tv_right:             //管理
                BankCardDetailDialog bankCardDetailDialog = BankCardDetailDialog.newInstance();
                bankCardDetailDialog.setOnDialogClickListener(BankCardDetailActivity.this);
                Bundle bundle = new Bundle();
                bundle.putBoolean("isEmptydata",isNull);
                bankCardDetailDialog.setArguments(bundle);
                bankCardDetailDialog.show(getSupportFragmentManager(),"bankcardDialog");
                break;
            default:
                break;
        }
    }

    private boolean isEmptydata(HSBankCardBean hsBankCardBean){
        if(hsBankCardBean != null){
            return true;
        }
        return false;
    }


    //添加银行卡
    @Override
    public void addBankCard() {

        if(findUserBasicInfoBean != null){
            if(findUserBasicInfoBean.getIsOpenAccount() != 0){          //是否新网银行开户授权0：是，1：否
                Bundle bundle = new Bundle();
                bundle.putSerializable(ConstantAppInfo.BASIC_INFO_BEAN, findUserBasicInfoBean);
                startActivity(BindBankCardActivity.class, bundle);
                return;
            }
        }
        startActivity(HSBindBankCardActivity.class);

    }

    //解绑银行卡
    @Override
    public void unbundledBankCard() {
        startActivity(HSUnBindBankCardActivity.class);
    }

    //修改支付密码
    @Override
    public void changePaypsw() {
        startActivity(HSupBankCardPswActivity.class);
    }

    //修改银行卡预留手机号码
    @Override
    public void changePhoneNub() {
        startActivity(HSupBankCardPhoneActivity.class);
    }




}
